package studentinformationmanagementsystem.dao.impl;

import studentinformationmanagementsystem.dao.BaseDao;
import studentinformationmanagementsystem.dao.TeacherDao;
import studentinformationmanagementsystem.entity.Teacher;

import java.util.List;

/**
 * @author 苗庆
 * @version 1.0
 */
public class TeacherDaoImpl extends BaseDao implements TeacherDao {

    /**
     * 对老师信息进行添加
     * @param teacher  老师对象
     * @return  受影响的记录行数
     */
    @Override
    public int insert(Teacher teacher) {
        String sql = "INSERT INTO `t_teacher` (`teachername`, `idnumber`, `sex`, `phone`, `politicsstatus`, `nation`, `photograph`," +
                " `mailbox`, `birthdate`, `nativeplace`,`homeaddress`,`teachingage`,`education`,`entrytime`,`individualresume`,`password`," +
                "`Teachersworknumber`,`IsDelete`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)";
        return super.update(sql,teacher.getTeachername(),teacher.getIdnumber(),teacher.getSex(),teacher.getPhone(),teacher.getPoliticsstatus(),
                teacher.getNation(),teacher.getPhotograph(),teacher.getMailbox(),teacher.getBirthdate(),teacher.getNativeplace(),teacher.getHomeaddress(),
                teacher.getTeachingage(),teacher.getEducation(),teacher.getEntrytime(),teacher.getIndividualresume(),teacher.getPassword(),teacher.getTeachersworknumber(),
                 teacher.getIsDelete());
    }


    /**
     *  根据ID更新老师信息
     * @param teacher  老师对象
     * @return 受影响的记录行数
     */
    @Override
    public int update(Teacher teacher) {
        String sql = "UPDATE `t_teacher` SET `teachername` = ? , `idnumber` = ? , `sex` = ? , `phone` = ? , `politicsstatus` = ? , " +
                "`nation` = ? ,`photograph` = ? , `mailbox` = ? , `birthdate` = ? , `nativeplace` = ?  ,`homeaddress` = ? ,`teachingage` = ?," +
                "`education` = ? ,`entrytime` = ? ,`individualresume` = ? ,`password` = ? ,`Teachersworknumber` = ? ,`IsDelete` = ? WHERE `teacherid` = ? ";
        return super.update(sql,teacher.getTeachername(),teacher.getIdnumber(),teacher.getSex(),teacher.getPhone(),teacher.getPoliticsstatus(),
                teacher.getNation(),teacher.getPhotograph(),teacher.getMailbox(),teacher.getBirthdate(),teacher.getNativeplace(),teacher.getHomeaddress(),
                teacher.getTeachingage(),teacher.getEducation(),teacher.getEntrytime(),teacher.getIndividualresume(),teacher.getPassword(),
                teacher.getTeachersworknumber(),teacher.getIsDelete(),teacher.getTeacherid());
    }


    /**
     *  根据ID删除老师
     * @param teacher 老师ID
     * @return 受影响的记录行数
     */
    @Override
    public int delete(Teacher teacher) {
        String sql = "DELETE FROM `t_teacher` WHERE `teacherid` = ? AND `IsDelete`=0";
        return super.update(sql, teacher.getTeacherid());
    }

    /**
     * 查询所有老师信息
     * @return 所有用户信息
     */
    @Override
    public List<Teacher> queryAll(Teacher teacher) {
        String sql = "SELECT * FROM `t_teacher` WHERE `IsDelete` = ?";
        return super.queryForList(Teacher.class, sql,teacher.getIsDelete());
    }

    /**
     * 根据老师ID查询学生信息
     * @param teacher 老师ID
     * @return 老师信息
     */
    @Override
    public Teacher queryById(Teacher teacher) {
        String sql = "SELECT * FROM `t_teacher` WHERE `teacherid` = ? AND `IsDelete`=0";
        return super.queryForOne(Teacher.class, sql, teacher.getTeacherid());
    }

    @Override
    public Teacher queryById01(Teacher teacher) {
        String sql = "SELECT * FROM `t_teacher` WHERE `teacherid` = ?";
        return super.queryForOne(Teacher.class, sql, teacher.getTeacherid());
    }

    /**
     * 根据教师工号即账号查询用户信息
     * @param teacher 教师工号
     * @return 教师信息
     */
    @Override
    public Teacher queryByUsername(Teacher teacher) {
        String sql = "SELECT * FROM `t_teacher` WHERE `Teachersworknumber` = ? AND `IsDelete`=0";
        return super.queryForOne(Teacher.class, sql, teacher.getTeachersworknumber());
    }


    /**
     * 根据教师工号即账号和密码查询用户信息
     * @param teacher 带有教师工号和密码的老师信息
     * @return 教师信息
     */
    @Override
    public Teacher queryByUsernameAndPassword(Teacher teacher) {
        String sql = "SELECT * FROM `t_teacher` WHERE `Teachersworknumber` = ? AND `password` = ? AND `IsDelete`=0";
        return super.queryForOne(Teacher.class, sql, teacher.getTeachersworknumber(), teacher.getPassword());
    }


    /**
     * 查询总记录条数
     */
    @Override
    public Integer queryForPageTotalCount() {
        String sql = "SELECT COUNT(1) FROM `t_teacher` WHERE `IsDelete`=0";
        Number count = (Number) queryForSingleValue(sql);
        return count.intValue();
    }


    /**
     * 分页查询DAO层方法
     *
     * @param begin    起始记录行数
     * @param pageSize 每页展示数量
     * @return
     */
    @Override
    public List<Teacher> queryForPageItems(int begin, int pageSize) {
        String sql = "SELECT * FROM `t_teacher` WHERE `IsDelete`=0 LIMIT ?, ?";
        return super.queryForList(Teacher.class, sql, begin, pageSize);
    }
}
